package com.samsungmobile.common_module.Parser;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.samsungmobile.common_module.Bean.SendMTMResult;

public class SendMTMResultParser extends DefaultHandler {

	private InputStream responseContent;
	private String responseContentString;

	public String getResponseContentString() {
		return responseContentString;
	}

	public void setResponseContentString(String responseContentString) {
		this.responseContentString = responseContentString;
	}


	private SendMTMResult sendMTMResult;
	private StringBuffer  sbfCharacter = new StringBuffer();
	
	public InputStream getResponseContent() {
		return responseContent;
	}

	public void setResponseContent(InputStream responseContent) {
		this.responseContent = responseContent;
	}
	
	public SendMTMResult getSendMTMResult() {
		return sendMTMResult;
	}

	public void setSendMTMResult(SendMTMResult sendMTMResult) {
		this.sendMTMResult = sendMTMResult;
	}

	public void process() throws ParserConfigurationException, SAXException{
		processStream();
	}
	
	public void processStream() throws ParserConfigurationException, SAXException{
		 SAXParserFactory factory   = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        
        sendMTMResult = new SendMTMResult();

        // parse the response input stream
        try {
			saxParser.parse(this.responseContent,this);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void processString() throws ParserConfigurationException, SAXException{
		 SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        
        sendMTMResult = new SendMTMResult();

        // parse the response input stream
        try {
			saxParser.parse(new InputSource(new StringReader(this.responseContentString)),this);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}	
	
	/*
	* Called when the Parser starts parsing the Current XML File.
	*/
	public void startDocument () throws SAXException
	{
		//System.out.println("startDocument");
	}

	/*
	 * Called when the Parser Completes parsing the Current XML File.
	*/
	public void endDocument () throws SAXException
	{
		//System.out.println("endDocument");
	}

	/*
	 * Called when the starting of the Element is reached.
	*/
	@Override 
	public void startElement( String namespaceURI, String localName, 
	                            String qName, Attributes atts ) throws SAXException 

	{
		sbfCharacter.setLength(0);
	}

	/*
	 * Called when the Ending of the current Element is reached. 
	*/
	@Override 
	public void endElement(String namespaceURI, String localName, String qName)
	{
		String val = sbfCharacter.toString();
		
		if(qName.equals("responseId")) {
			sendMTMResult.setResponseId(val);
		}
		else if(qName.equals("messageId")) {
			sendMTMResult.setMessageId(val);
		}
		else if(qName.equals("responseCode")) {
			if(val != null && val.length() > 0) {
				sendMTMResult.setResponseCode(Integer.parseInt(val));
			}
		}
		else if(qName.equals("responseMessage")) {
			sendMTMResult.setResponseMessage(val);
		}

		sbfCharacter.setLength(0);
	}

	/*
	 * While Parsing the XML file, if extra characters like space or enter Character
	 * are encountered then this method is called.
	*/
	public void characters (char buf [], int offset, int length) throws SAXException
	{
		sbfCharacter.append(buf, offset, length);
	}


	public void processingInstruction (String target, String data) throws SAXException
	{

	} 
}

